﻿
using HBase;
using System.Collections.Generic;
namespace hCommand {
    public abstract class AbsWcs2WmsTask :ZtTask {

        protected IWCSServer wcsServer = WMSDalFactray.getDal<IWCSServer>();

        void finshParam() {//方法 taskArrived
            List<CoreTaskParam> paramList = MyCache.getList<CoreTaskParam>();
            foreach( CoreTaskParam p in paramList ) {
                Node node = MyCache.queryUnique<Node>((a) => {
                    return a.isConfig(p.des);
                } ,"CompleteParam des 过多" + p.des);

                if (node == null){
                    continue;
                }
                Estimate fe = node.getCurrent();
                if( fe != null && fe.taskNo == p.taskNo ) {
                    p.status = ( int ) CoreTaskParamStatus.执行结束;
                    p.cDes = p.des;
                    fileAll(InfoCode.fileAll_node_arrived, "arrived-is-finsh");
                }
            }
        }
        public override bool excute() {


            finshParam();

            List<CoreTaskParam> list = MyCache.getList<CoreTaskParam>();
            foreach( CoreTaskParam param in list ) {
                if (param.des == TaskNoUtil.virtual_Loc) {
                      continue; 
                }
                bool res = false;
                if (param.status == (int)CoreTaskParamStatus.满放变更) {
                    res = putFull(param);
                } else if (param.status == (int)CoreTaskParamStatus.空取) {
                    res = getEmply(param);
                } else if (isFinsh(param)) {
                    if (commit(param)) {
                        Node node = MyCache.queryUnique<Node>((a) => {
                            return a.isConfig(param.des);
                        }, "CompleteParam des 过多" + param.des);

                        Estimate e = node.getCurrent();
                        if (e != null && e.taskNo == param.taskNo) {
                            node.clearCurrent(e.getId());
                        }
                        PathFacade.removeParam(param);
                    }

                } else {
                    working(param);
                }

            }
            return true;
        }

        public virtual bool working(CoreTaskParam param) {
            
            return false;
        }
        public virtual bool isFinsh(CoreTaskParam param) {
            if( param.status == ( int ) CoreTaskParamStatus.执行结束 ) {
                return param.cDes == param.des || param.cStn == param.des;
            }
            return false;
        }
        public abstract bool putFull(CoreTaskParam param);
        public abstract bool getEmply(CoreTaskParam param);

        public abstract bool commit(CoreTaskParam param);
    }
}
